package com.hangsu.train.components.web.controller;

import com.hangsu.train.components.convert.PermissConvert;
import com.hangsu.train.components.entity.Permiss;
import com.hangsu.train.components.params.CodeParam;
import com.hangsu.train.components.params.IdParam;
import com.hangsu.train.components.params.PermissParam;
import com.hangsu.train.components.params.PermissUpdateParam;
import com.hangsu.train.components.service.PermissService;
import com.hangsu.train.components.vo.PermissVo;
import com.hangsu.train.core.result.Resp;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permiss")
public class PermissController {

    private PermissService permissService;
    private PermissConvert permissConvert;

    @Resource
    public void setPermissService(PermissService permissService) {
        this.permissService = permissService;
    }

    @Resource
    public void setPermissConvert(PermissConvert permissConvert) {
        this.permissConvert = permissConvert;
    }

    /**
     * 增加权限
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/create")
    public Resp create(@RequestBody @Valid PermissParam param) {
        permissService.create(param);
        return Resp.ok();
    }

    /**
     * 修改权限
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/update")
    public Resp update(@RequestBody @Valid PermissUpdateParam param) {
        permissService.update(param);
        return Resp.ok();
    }

    /**
     * 查看权限
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/detail")
    public Resp detail(@RequestBody @Valid IdParam param) {
        Permiss respDto = permissService.detail(param.getId());
        return Resp.ok(respDto);
    }

    /**
     * 删除权限
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/del")
    public Resp del(@RequestBody @Valid IdParam param) {
        permissService.del(param.getId());
        return Resp.ok();
    }

    /**
     * 获取全部权限
     *
     * @return
     */
    @PostMapping(value = "/list")
    public Resp<List<PermissVo>> getAll() {
        List<PermissVo> permissVoList  = permissService.getAllPermiss();
        return Resp.ok(permissVoList);
    }

    /**
     * 获取角色权限
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/role/list")
    public Resp getRolePermiss(@RequestBody @Valid CodeParam param) {
        List<Permiss> respDtoList = permissService.getRolePermiss(param.getCode());
        return Resp.ok(respDtoList);
    }

}
